package com.easybuy.service.UserAddressService;

import com.easybuy.dao.UserAddressMapper;
import com.easybuy.pojo.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService{
    @Autowired
    private UserAddressMapper userAddressMapper;

    //查询全部地址信息
    public List<UserAddress> UserAddressList() {
        return userAddressMapper.UserAddressList();
    }

    //查询用户地址信息
    public List<UserAddress> getUserAddress(int userId) {
        return userAddressMapper.getUserAddress(userId);
    }

    //修改地址信息
    public Integer modifyUserAddress(UserAddress userAddress) {
        if (userAddress.getIsDefault()==1){
            this.cleanDefaultUserAddress(userAddress.getUserId());
        }
        return userAddressMapper.modifyUserAddress(userAddress);
    }

    //设置默认地址
    public Integer isDefaultUserAddress(Integer id) {
        int count =0;
        try{
            count = userAddressMapper.isDefaultUserAddress(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    //清除默认地址
    public Integer cleanDefaultUserAddress(Integer userId){
        int count =0;
        try{
            count = userAddressMapper.cleanDefaultUserAddress(userId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    //增加地址
    public Integer addUserAddress(UserAddress userAddress){
        if (userAddress.getIsDefault()==1){
            this.cleanDefaultUserAddress(userAddress.getUserId());
        }
        return userAddressMapper.addUserAddress(userAddress);
    }

    //通过id查询地址
    public UserAddress getUserAddressById(int id) {
        return userAddressMapper.getUserAddressById(id);
    }

    //删除地址
    public int deleAddress(int id) {
        return userAddressMapper.deleAddress(id);
    }

}
